package cn.stimd.spring.beans.factory.annotation;

import cn.stimd.spring.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;

/**
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition {
    private final AnnotationMetadata metadata;

    //使用反射的方式解析类
    public AnnotatedGenericBeanDefinition(Class<?> beanClass) {
        setBeanClass(beanClass);
        this.metadata = new StandardAnnotationMetadata(beanClass, true);
    }


    //通过反射或ASM 的方式解析类
    public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata) {
        //通过反射方式解析，Class信息已知
        if (metadata instanceof StandardAnnotationMetadata) {
            setBeanClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass());
        }
        //通过ASM方式解析，Class信息未知
        else {
            setBeanClassName(metadata.getClassName());
        }
        this.metadata = metadata;
    }

    @Override
    public AnnotationMetadata getMetadata() {
        return this.metadata;
    }

    @Override
    public MethodMetadata getFactoryMethodMetadata() {
        return null;
    }
}
